#!/usr/bin/python
#coding: utf-8

import Tkinter
import tkMessageBox
from Tkinter import *
from Process import *


def showMessage():
    tkMessageBox.showinfo("RESULT", "Download Completed")


def test():
    p = Process()
    if (p.test()):
        showMessage()


def down_lists(startpage, endpage):
    p = Process()
    p.record_news_pages(startpage, endpage)
    print u"Download Completed"


def down_text(threadnum=5,start=1,end=0):
    p = Process()
    p.extract_text_multi_threads(threadnum, start, end)
    print u"Download Completed"


def export_news_list():
    p = Process()
    filename = u'excel/total.xlsx'
    p.exportExcel(filename)


# top = Tkinter.Tk()
# top.title("MEKONG INFO DOWN")
# top.geometry('200x100')  # 是x 不是*
# top.resizable(width=False, height=True)  # 宽不可变, 高可变,默认为True
# frm = Frame(top)
# btn_down_items = Tkinter.Button(frm, text=u"下载新闻列表", command=down_lists).pack(side=TOP)
# btn_down_files = Tkinter.Button(frm, text=u"下载文章", command=test).pack(side=TOP)
# frm.pack(side=LEFT)
# top.mainloop()

while (True):
    print u"[1] 下载新闻列表"
    print u"[2] 下载新闻正文"
    print u"[3] 导出新闻列表"
    print u"[0] 退出"
    print u"[0000] 返回主菜单"
    print u"请选择要进行的操作:"
    operate = raw_input()
    if (operate == "0"):
        sys.exit()
    elif (operate == '1'):
        print u"请输入开始页 startpage:"
        startpage = raw_input()
        if startpage == "0000":continue
        print u"请输入结束页 endpage:"
        endpage = raw_input()
        if endpage == "0000":continue
        down_lists(int(startpage), int(endpage))
    elif (operate == '2'):
        print u"设置线程个数,默认为5个"
        threadnum = raw_input()
        if threadnum == "0000":continue
        print u"设置开始下载位置，默认为1"
        start = raw_input()
        if start == "0000":continue
        print u"设置终止下载位置，输入0，表示下载全部"
        end = raw_input()
        if end == "0000":continue
        down_text(threadnum=int(threadnum),start=int(start),end=int(end))
    elif (operate == '3'):
        export_news_list()
